package com.ydgk.servlet;

import com.ydgk.bean.Book;
import com.ydgk.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BookPagingServlet", urlPatterns = "/BookPagingServlet.action")
public class BookPagingServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String pageNumber = request.getParameter("pageNumber");
        BookDao bookDao = new BookDao();
        int page = 0;
        try{
            page = Integer.parseInt(pageNumber);
        }catch (NumberFormatException e){
            page = 1;
        }
        int maxPage = bookDao.maxPage("book");
        if(page>maxPage){
            page = maxPage;
        }else if(page<0){
            page = 1;
        }
        List<Book> booklist = bookDao.paging(page);
        request.setAttribute("booklist",booklist);
        request.setAttribute("page",page);
        request.setAttribute("maxPage",maxPage);
        request.getRequestDispatcher("books.jsp").forward(request,response);
    }


}
